using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.PlayerLoop;

public class BulletW4 : MonoBehaviour
{
    public float speed;
    public float lifeTime;
    public float distance;
    public int damage;
    private GameObject player;
    private GameObject SecondPlayer;
    public LayerMask whatIsSolid;

    public GameObject bulletEffect;


    private void Start() {   
        //Поворот спрайта на 90 градусов.
        player = GameObject.FindGameObjectWithTag("Player");
        SecondPlayer = GameObject.FindGameObjectWithTag("SecondPlayer");

        //Уничтожение пули по определённому времени
        Invoke("DestroyBullet", lifeTime);
    }

    private void Update() {   
        RaycastHit2D hitInfo = Physics2D.Raycast(transform.position, Vector2.down, distance, whatIsSolid);

        //Проверка на сталкивание с препятствием, имеющим свойство Solid.
        if (hitInfo.collider != null) {
            if (hitInfo.collider.CompareTag("EnemyShooter")) {
                hitInfo.collider.GetComponent<EnemyShooter>().TakeDamage(damage);
            }
            if (hitInfo.collider.CompareTag("Enemy")) {
                hitInfo.collider.GetComponent<EnemyRunner>().TakeDamage(damage);
            }
            DestroyBullet();
        }        

        // Перемещение пули
        transform.Translate(Vector2.up * speed * Time.deltaTime);
    }

    public void DestroyBullet() {   
        Instantiate(bulletEffect, transform.position, Quaternion.identity);
        Destroy(gameObject);
    }
}
